package com.gxs.sample.restbeans;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.gxs.ea.fileporta.data.services.CompanyService;
import com.gxs.ea.fileportal.domain.util.ErrorResponse;
import com.gxs.ea.fileportal.model.persisted.Company;

@Component("companyRestBean")
@Path("/companies")
public class CompanyRestBean 
{
	@Autowired
	private CompanyService companyService;
	
	@GET
	@Path("/{companyCode}")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getCompanyByCode(@PathParam("companyCode") String companyCode)
	{
		Company company = this.companyService.findByCompanyCode(companyCode);
		
		if(company== null)
		{
			ErrorResponse response  = new ErrorResponse();
			response.setErrorSummary("No company with code: " + companyCode + " was found");
			response.setErrorDetails("No company with code: " + companyCode + " was found");
			response.setErrorSubCode("GXS-404");
			
			return Response.status(404).entity(response).build();
		}
		else
		{
			return Response.status(200).entity(company).build();
		}
	}


	@POST
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public Response saveCompany(Company company)
	{
		try
		{
			Company savedCompany = this.companyService.saveCompany(company);
			return Response.status(200).entity(savedCompany).build();
		}
		catch(Exception e)
		{
			ErrorResponse response = new ErrorResponse();
			response.setErrorSummary("Error on saving company: " + e.getMessage());
			response.setErrorDetails(ExceptionUtils.getFullStackTrace(e));
			response.setErrorSubCode("GXS-500");
			
			//
			// We are returning 500 but in proper implementation we need to 
			// figure out if this was client data error and use 400 series or
			// 500 serires error accordingly. 
			return Response.status(500).entity(response).build();
		}
	}
	
	
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public Response getAllCompanies()
	{
		List<Company> list = this.companyService.listCompanies();
		return Response.status(200).entity(list).build();
	}
	
	
	@DELETE
	@Path("/{companyCode}")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public Response deleteCompany(@PathParam("companyCode") String companyCode)
	{
		Company company = this.companyService.findByCompanyCode(companyCode);
		
		if(company== null)
		{
			ErrorResponse response  = new ErrorResponse();
			response.setErrorSummary("No company with code: " + companyCode + " was found");
			response.setErrorDetails("No company with code: " + companyCode + " was found");
			response.setErrorSubCode("GXS-404");
			
			return Response.status(404).entity(response).build();
		}
		else
		{
			try
			{
				this.companyService.deleteCompany(company);
				return Response.status(204).build();
			}
			catch(Exception e)
			{
				ErrorResponse response = new ErrorResponse();
				response.setErrorSummary("Error on deleting company: " + e.getMessage());
				response.setErrorDetails(ExceptionUtils.getFullStackTrace(e));
				response.setErrorSubCode("GXS-500");
				
				//
				// We are returning 500 but in proper implementation we need to 
				// figure out if this was client data error and use 400 series or
				// 500 serires error accordingly. 
				return Response.status(500).entity(response).build();
			}
		}
	}
	
	@PUT
	@Path("{companyCode}")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public Response updateCompany(Company company)
	{
		try
		{
			this.companyService.updateCompany(company);
			return Response.status(200).entity(company).build();
		}
		catch(Exception e)
		{
			ErrorResponse response = new ErrorResponse();
			response.setErrorSummary("Error on updating company: " + e.getMessage());
			response.setErrorDetails(ExceptionUtils.getFullStackTrace(e));
			response.setErrorSubCode("GXS-500");
			
			//
			// We are returning 500 but in proper implementation we need to 
			// figure out if this was client data error and use 400 series or
			// 500 serires error accordingly. 
			return Response.status(500).entity(response).build();
			
		}
		
	}
	
	public CompanyService getCompanyService() {
		return companyService;
	}

	public void setCompanyService(CompanyService companyService) {
		this.companyService = companyService;
	}
	
	
	

}
